package stackdemo;

import java.util.LinkedList;
import java.util.Stack;

/**
 * @Author 12629
 * @Description：
 */
public class Test {

    public boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i = 0;i < pushV.length;i++) {
            stack.push(pushV[i]);
            while(!stack.isEmpty() && j < popV.length
                    && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }
        }
        return stack.isEmpty();
    }

    public boolean isValid(String s) {

        Stack<Character> stack = new Stack<>();
        for(int i = 0;i < s.length();i++) {
            char ch = s.charAt(i);
            if(ch == '(' || ch == '{' || ch == '[') {
                stack.push(ch);
            }else {
                //ch = 右括号
                if(stack.isEmpty()) {
                    return false;
                }
                //拿到栈顶元素
                char peekCh = stack.peek();
                if(peekCh=='(' && ch == ')'
                        || peekCh=='{' && ch == '}'
                        || peekCh=='[' && ch == ']') {
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        if(!stack.isEmpty()) {
            return false;
        }

        return true;
    }

    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(int i = 0;i < tokens.length;i++){
            String s = tokens[i];//"12"
            if(!isOperation(s)) {
                //数字
                Integer ret = Integer.valueOf(s);
                stack.push(ret);
            }else {
                Integer val2 = stack.pop();
                Integer val1 = stack.pop();
                switch(s) {
                    case "+":
                        stack.push(val1 + val2);
                        break;
                    case "-":
                        stack.push(val1 - val2);
                        break;
                    case "*":
                        stack.push(val1 * val2);
                        break;
                    case "/":
                        stack.push(val1 / val2);
                        break;
                }
            }
        }
        return stack.pop();
    }
    private boolean isOperation(String val) {
        if(val.equals("+") || val.equals("-") || val.equals("*") || val.equals("/")) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        LinkedList<Integer> stack = new LinkedList<>();

        stack.push(1);
        stack.push(2);
        System.out.println(stack.pop());

    }
    public static void main3(String[] args) {
        MyStack stack = new MyStack();
        stack.push(12);
        stack.push(23);
        stack.push(34);
        stack.push(45);

        int val = stack.pop();//删除
        System.out.println(val);//45
        val = stack.pop();
        System.out.println(val);//34

        int val2 = stack.peek();//只获取 不删除
        System.out.println(val2);//23

        val2 = stack.peek();//只获取 不删除
        System.out.println(val2);//23

        boolean empty = stack.isEmpty();
        System.out.println(empty);//false

    }
    public static void main1(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(12);
        stack.push(23);
        stack.push(34);
        stack.push(45);
        int val = stack.pop();//删除
        System.out.println(val);
        val = stack.pop();
        System.out.println(val);

        int val2 = stack.peek();//只获取 不删除
        System.out.println(val2);

        val2 = stack.peek();//只获取 不删除
        System.out.println(val2);

        boolean empty = stack.empty();
        System.out.println(empty);

        //stack.contains()
    }
}
